import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;


public class requestInterface extends JFrame implements ActionListener
{
   private JButton reNew = new JButton("Another Driver?");
   private Container contents1;

   private JLabel statusReq;
   private JLabel holidayStatus;
   private JTextField startDate = new JTextField(20);
   private JTextField endDate = new JTextField(20);
   JButton reqHoliday = new JButton("Request Holiday");
   //driverLogedIn
   int driverId;

   public requestInterface(int driverID)
   {
      contents1 = getContentPane();
      setTitle("Request Holiday");
      driverId=driverID;
      contents1.setLayout(new GridLayout(0, 1));
      JPanel contents2 = new JPanel(new GridLayout(0, 2));
      JPanel contents3 = new JPanel(new GridLayout(0, 2));
      JPanel contents4 = new JPanel(new GridLayout(0, 2));
      JPanel contents5 = new JPanel(new GridLayout(0, 2));
      contents1.add(contents2, BorderLayout.SOUTH);
      contents1.add(contents3, BorderLayout.SOUTH);
      contents1.add(contents4, BorderLayout.SOUTH);
      contents1.add(contents5, BorderLayout.SOUTH);
      contents2.add(new JLabel("Welcome " + DriverInfo.getName(driverID)));
      holidayStatus = new JLabel("Holidays taken: " +
	  DriverInfo.getHolidaysTaken(driverID) + "     Remain days: " +
	  (25-DriverInfo.getHolidaysTaken(driverID)));
      contents2.add(holidayStatus);
      contents3.add(new JLabel("Start Date of holiday:  dd/mm/yyyy"));
      contents3.add(startDate);
      contents4.add(new JLabel("End Date of holiday:    dd/mm/yyyy"));
      contents4.add(endDate);
      contents5.add(reqHoliday);      reqHoliday.addActionListener(this);
      contents5.add(reNew); reNew.addActionListener(this);
      statusReq = new JLabel("status");
      contents1.add(statusReq);
      // exit the program in case the window it's closed
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      pack();
   }//cont 

   private DLoginInterface first;
   public void actionPerformed(ActionEvent event)
   {
      //for driver request
      if(event.getSource()==reqHoliday)
      { //check if NULL
        String x = startDate.getText(),y = endDate.getText();
        if(x.equals("")||y.equals(""))
	{
	  statusReq.setText("Start date and end date can not be empty!");
	  return;
	}
	
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date test_date1 = null;
	Date test_date2 = null;
      	try
	{
	  test_date1 = df.parse(x);
          test_date2 = df.parse(y);
	}
	catch(ParseException error)
        {
	  statusReq.setText("The format of Start date or end date is wrong!");
	  return;
	}
	if (!df.format(test_date1).equals(x))
	{
	  statusReq.setText("Start date is invalid day!");
	  return;
	}
	else if (!df.format(test_date2).equals(y))
	{
	  statusReq.setText("End date is invalid day!");
	  return;
	}

 	String[] startWords=startDate.getText().split("/");
	String[] endWords = endDate.getText().split("/");
        

	if(startWords.length<=3&&endWords.length<=3)
        {
 		try {
		  int day = Integer.parseInt(startWords[0]);int month = Integer.parseInt(startWords[1])-1;int year = Integer.parseInt(startWords[2]);
		  Date start = new GregorianCalendar(year,month,day).getTime();
     		  Date end = new GregorianCalendar(Integer.parseInt(endWords[2]), Integer.parseInt(endWords[1])-1, Integer.parseInt(endWords[0])).getTime();
      		  //check constraints usign methods in the Controller class	
        	  statusReq.setText(Controller.checkReq(start,end,driverId));
		  holidayStatus.setText("Holidays taken: " + 
		      DriverInfo.getHolidaysTaken(driverId) + "     Remain days: " +
		      (25-DriverInfo.getHolidaysTaken(driverId)));
 		}//try
                catch(NumberFormatException error)
                { 
 		   statusReq.setText("holidays rejected due to invalid dates");
                   return ;   
                }
        }//if	
      }//if reqHoliday
      if(event.getSource()==reNew)
      {    
            first =new  DLoginInterface();
            first.setVisible(true);
            dispose();
      }
   } // actionPerformed

  
} // class
